<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-loading="show">
          <el-tab-pane label="角色管理">
            <el-row style="height: 60px">
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="small"
                @click="addRole"
              >新增角色</el-button>
            </el-row>
            <el-table border="" :data="rows">
              <el-table-column label="序号" width="120px" type="index" align="center" />
              <el-table-column label="名称" width="240px" prop="name" align="center" />
              <el-table-column label="描述" prop="description" align="center" />
              <el-table-column label="操作" align="center">
                <template slot-scope="{ row }">
                  <el-button size="small" type="success" :loading.sync="btnIndex === row.id && distributeBtnLoading" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="onEdit(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="onDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row style="height:60px" type="flex" justify="center" align="middle">
              <el-pagination
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                layout="prev,pager,next"
                @current-change="onChangePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改" :closable="false" />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="formData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="电话">
                <el-input v-model="formData.companyPhone" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <!-- 编辑角色弹层 -->
      <el-dialog
        title="编辑角色"
        :visible="editShow"
        width="800px"
        @close="btnCancel"
      >
        <el-form ref="editForm" :model="editForm" :rules="rules" label-width="120px">

          <el-form-item prop="name" label="角色名称">
            <el-input v-model="editForm.name" style="width:500px" />
          </el-form-item>

          <el-form-item prop="description" label="角色描述">
            <el-input v-model="editForm.description" style="width:500px" />
          </el-form-item>

        </el-form>

        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="4">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <!-- 分配权限弹层 -->
      <el-dialog title="分配权限" :visible="distibutePermPop" @close="cancleDistibute">
        <el-tree
          ref="perTree"
          :data="permData"
          :props="{label: 'name'}"
          :default-expand-all="true"
          :show-checkbox="true"
          :check-strictly="true"
          node-key="id"
          :default-checked-keys="permIds"
        />
        <el-row slot="footer" type="flex" justify="center" align="midlle">
          <el-col :span="4">
            <el-button size="small" @click="cancleDistibute">取消</el-button>
            <el-button type="primary" size="small" @click="confirmDistibute">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 分配权限弹层 -->
    </div>
  </div>
</template>

<script>
import { getRoleListAPI, getCompanyInfoAPI, delRoleAPI, getRoleInfoAPI, updateRoleInfoAPI, addRoleAPI, getPermissionListAPI, assignPermissionAPI } from '@/api'
import { transformListToTree } from '@/utils/index'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      roleID: null,
      btnIndex: '',
      permIds: [],
      permData: [],
      distibutePermPop: false,
      page: {
        page: 1,
        pagesize: 10,
        total: 0
      },
      rows: [],
      formData: {},
      editForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, triggle: 'blur', message: '名字不能为空' }]
      },
      editShow: false,
      show: false,
      distributeBtnLoading: false
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getRoleListAPI()
    setTimeout(this.getCompanyInfoAPI, 1000)
  },
  methods: {

    async getRoleListAPI() {
      this.show = true
      const { total, rows } = await getRoleListAPI(this.page)
      this.page.total = total
      this.rows = rows
      this.show = false
    },

    onChangePage(newpage) {
      this.page.page = newpage
      this.getRoleListAPI()
    },

    // 获取公司信息
    async getCompanyInfoAPI() {
      console.log(this.companyId)
      this.formData = await getCompanyInfoAPI(this.companyId)
    },

    async onDel(id) {
      try {
        await this.$confirm('确定要删除吗')
        await delRoleAPI(id)
        this.getRoleListAPI()
        this.$message.success('删除角色成功')
      } catch (err) {
        console.log(err)
      }
    },

    async onEdit(id) {
      try {
        this.editForm = await getRoleInfoAPI(id)
        this.editShow = true
      } catch (err) {
        console.log('出错了', err)
      }
    },

    btnCancel() {
      this.editShow = false
      this.$refs.editForm.resetFields()
      this.editForm = this.$options.data().editForm
    },

    btnOK() {
      this.$refs.editForm.validate(async isOK => {
        if (isOK) {
          try {
            if (this.editForm.id) {
              await updateRoleInfoAPI(this.editForm)
            } else {
              await addRoleAPI(this.editForm)
            }
            this.getRoleListAPI()
            this.editShow = false
          } catch (err) {
            console.log(err)
          }
        }
      })
    },

    addRole() {
      this.editShow = true
    },
    // 分配权限
    async assignPerm(id) {
      this.roleID = id
      this.btnIndex = id
      this.distributeBtnLoading = true
      this.permData = transformListToTree(await getPermissionListAPI(), '0')
      const { permIds } = await getRoleInfoAPI(id)
      this.permIds = permIds
      this.distibutePermPop = true
      this.distributeBtnLoading = false
    },
    // 确定分配按钮
    async confirmDistibute() {
      try {
        await assignPermissionAPI({ permIds: this.$refs.perTree.getCheckedKeys(), id: this.roleID })
        this.$message.success('分配权限成功')
        this.distibutePermPop = false
      } catch (err) {
        this.$message.error('分配权限失败')
      }
    },
    // 取消分配按钮
    cancleDistibute() {
      this.permIds = []
      this.distibutePermPop = false
    }
  }
}
</script>

<style>

</style>
